package cn.dcw.pethome.user.service.impl;

import cn.dcw.pethome.basic.exception.MyException;
import cn.dcw.pethome.basic.service.impl.BaseServiceImpl;
import cn.dcw.pethome.basic.util.MD5Utils;
import cn.dcw.pethome.basic.util.StrUtils;
import cn.dcw.pethome.user.domain.LoginInfo;
import cn.dcw.pethome.user.domain.User;
import cn.dcw.pethome.user.dto.UserDto;
import cn.dcw.pethome.user.mapper.LoginInfoMapper;
import cn.dcw.pethome.user.mapper.UserMapper;
import cn.dcw.pethome.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Map;

/*
普通用户管理
 */
@Service

public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    //加载部门mapper
    @Autowired
    private UserMapper mapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    //注入redis对象
    @Autowired
    private RedisTemplate redisTemplate;

    //添加部门
    @Override
    @Transactional
    public void add(User user) {
        mapper.add(user);
       this.update(user);
    }


    @Override
    @Transactional
    public void update(User user) {

        mapper.update(user);
    }

    /**
     * 用户账号注册
     * @param userDto
     */
    @Override
    public void register(UserDto userDto) {
        //1.判断非空
        if(StringUtils.isEmpty(userDto.getPhone())){
            throw new MyException("手机号不能为空***");
        }
        if(StringUtils.isEmpty(userDto.getCode())){
            throw new MyException("验证码不能为空***");
        }
        if(StringUtils.isEmpty(userDto.getPassword())){
            throw new MyException("密码不能为空***");
        }
        if(StringUtils.isEmpty(userDto.getPasswordRepeat())){
            throw new MyException("确认密码不能为空***");
        }
        //2.校验验证码是否一致
        //2.1获取redis中储存的验证码
        //obj  code-时间戳
        Object obj = redisTemplate.opsForValue().get("register-" + userDto.getPhone());
        if(obj==null){
            throw new MyException("验证码过期，请重新获取***");
        }
        //验证码
        String redisCode = obj.toString().split("-")[0];
        //equalsIgnoreCase不区分大小写比较
        if(!redisCode.equalsIgnoreCase(userDto.getCode())){
            throw new MyException("验证码输入有误***");
        }
        //3.两次输入的密码必须一致
        if(!userDto.getPassword().equals(userDto.getPasswordRepeat())){
            throw new MyException("请确认两次输入的密码一致***");
        }
        //4.调用方法，将接收的对象转换为储存到数据库的user对象
        User user = userDto2User(userDto);
        //4.1获取loginInfo对象
        LoginInfo loginInfo = user2LoginInfo(user);
        loginInfo.setType(1);
        //5.LoginInfo添加数据,返回自增长主键
        loginInfoMapper.add(loginInfo);

        //6.添加数据到user表中
        user.setLogininfo_id(loginInfo.getId());
        mapper.add(user);
    }

    //将接收的对象转换为储存到数据库的user对象的方法
    public User userDto2User(UserDto userDto){
        //先创建一个user对象
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        //调用方法生成随机盐
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        //客户输入的密码+盐值
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(password);
        return user;
    }
    //获取loginInfo对象方法
    public LoginInfo user2LoginInfo(User user){
        LoginInfo loginInfo = new LoginInfo();
        //通过bean属性方法复制对象
        BeanUtils.copyProperties(user,loginInfo);
        return loginInfo;
    }

}
